'use strict'

const config = require('../../config/config.js')

let auth

if (config.api.mode === 'token') {
  auth = require('titbit-token')
} else {
  let {jwt} = require('titbit-toolkit')
  auth = jwt
}

let m = new auth({
  key: config.api.key,
  expires: config.api.expires > 0 ? config.api.expires : 3600000
})

module.exports = [
  {
    pre: true,
    middleware: m
  },

  {
    pre: true,
    middleware: async (c, next) => {
      if (config.api.aesAttr) {
        let atr = config.api.aesAttr
        c.box.user[atr] = c.helper.aesDescrypt(c.box.user[atr], config.api.aesKey)
      }

      await next()
    }
  }
]
